package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetBill(c *gin.Context)  {
	err, info, total := services.GetAllbill()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func DelBill(c *gin.Context){
	id := -1
	if arg := c.PostForm("billId"); arg != "" {
		id = com.StrTo(arg).MustInt()
	}
	if id == -1{
		app.INFO(c,30001,"参数错误")
		return
	}
	err := services.Delbill(id)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func UpdateBill(c *gin.Context){
	id := c.PostForm("billId")
	id2 := com.StrTo(id).MustInt()
	orderId := c.PostForm("orderId")
	consumerName := c.PostForm("consumerName")
	goodsName := c.PostForm("goodsName")
	sumMoney := c.PostForm("sumMoney")
	tempSumMoney := com.StrTo(sumMoney).MustInt()
	billClass := c.PostForm("billClass")
	billDate := c.PostForm("billDate")



	if id == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updatebill(models.Bill{ Bill_id: id2,  Order_id: orderId,  Consumer_name: consumerName,  Goods_name: goodsName,  Sum_money: tempSumMoney, Bill_class: billClass, Bill_date: billDate})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func AddBill(c *gin.Context){
	id := c.PostForm("billId")
	id2 := com.StrTo(id).MustInt()
	orderId := c.PostForm("orderId")
	consumerName := c.PostForm("consumerName")
	goodsName := c.PostForm("goodsName")
	sumMoney := c.PostForm("sumMoney")
	tempSumMoney := com.StrTo(sumMoney).MustInt()
	billClass := c.PostForm("billClass")
	billDate := c.PostForm("billDate")



	if id == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addbill(models.Bill{ Bill_id: id2,  Order_id: orderId,  Consumer_name: consumerName,  Goods_name: goodsName,  Sum_money: tempSumMoney, Bill_class: billClass, Bill_date: billDate})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}
